const Router = require('koa-router')
const { sequelize } = require('../database/db')
const { QueryTypes } = require('sequelize')
// const Op = require('sequelize').Op
const { Metadata } = require('../database/models/metadata')
// const check = require('../pem/check')

let router = new Router()

router.post('/table', async(ctx) => {
    const { id, pageSize, pageIndex } = ctx.request.body
    if (id && pageSize && pageIndex) {
        const data = await Metadata.findAndCountAll({
            where: {
                VirusTaxonomyID: id
            },
            raw: true,
            limit:  pageSize,
            offset: (pageIndex - 1) * pageSize
        });
        ctx.body = {
            code: 101,
            data,
            msg: ''
        }
    } else {
        ctx.body = {
            code: 102,
            data: null,
            msg: ''
        }
    }
})

router.post('/bi', async(ctx) => {
    const { id } = ctx.request.body
    if (id) {
        const data = await sequelize.query(
            `SELECT jsonString FROM Statistics WHERE vid = '${id}'`,
            { type: QueryTypes.SELECT }
        );
        ctx.body = {
            code: 101,
            data: JSON.parse(data[0].jsonString),
            msg: ''
        }
    } else {
        ctx.body = {
            code: 102,
            data: null,
            msg: ''
        }
    }
})

module.exports = router
